package nus.mtech.customer;

import java.util.Date;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;

import nus.mtech.util.DateUtil;
import nus.mtech.util.StringUtil;

/**
 * @author Kyaw Phyu Aung, Gaurav Pahwa
 *
 */
public class CustomerManager {

	private static CustomerManager objCustMgr;
	private static HashMap<String, Customer> customerMap = new HashMap<String, Customer>();
	private static int customerId = 1;

	// private final String CUST_STATUS_INACTIVE = "I";

	public static CustomerManager getCustomerManagerInstance() {
		if (null == objCustMgr) {
			objCustMgr = new CustomerManager();
		}
		return objCustMgr;
	}

	private CustomerManager() {
		super();
		objCustMgr = this;
	}

	public void addNewCustomer(Customer customer) {
		if (customerMap == null) {
			customerMap = new HashMap<String, Customer>();
		}
		customerMap.put(customer.getCustNRIC(), customer);
	}

	public void removeCustomer(String custNRIC) {
		Customer cust = this.getCustomerByNRIC(custNRIC);
		cust.setCustStatus(false);
	}

	public Customer getCustomerByNRIC(String custNRIC) {
		if (customerMap.containsKey(custNRIC)) {
			return customerMap.get(custNRIC);
		}
		return null;
	}

	public Customer getCustomerByAccountNo(String custAccountNo) {
		ArrayList<Customer> lstCust = this.getAllCustomers();
		for (Customer cust : lstCust) {
			if (cust.getAccountNo() != null
					&& cust.getAccountNo().equalsIgnoreCase(custAccountNo)) {
				return cust;
			}
		}
		return null;
	}

	public ArrayList<Customer> getAllCustomers() {
		ArrayList<Customer> lstCust = new ArrayList<Customer>();
		Iterator<Customer> custItr = customerMap.values().iterator();
		while (custItr.hasNext()) {
			lstCust.add(custItr.next());
		}
		return lstCust;
	}

	public static boolean customerExists(String custNRIC) {
		return customerMap.containsKey(custNRIC);
	}

	public int updateCustomerDetails(Customer cust) {
		int updtCount = 0;

		if (customerExists(cust.getCustNRIC())) {
			Customer custUpdate = this.getCustomerByNRIC(cust.getCustNRIC());

			custUpdate.setCustBillingAddr(cust.getCustBillingAddr());
			custUpdate.setCustName(cust.getCustName());
			custUpdate.setCustNRIC(cust.getCustNRIC());
			custUpdate.setCustTelNo(cust.getCustTelNo());
			custUpdate.setAccountNo(cust.getAccountNo());
			custUpdate.setCustStatus(cust.getCustStatus());
			custUpdate.setCustInterests(cust.getCustInterests());
			updtCount++;
		}
		return updtCount;
	}

	public int getCustomerId() {
		return customerId++;
	}

	public void setCustomerId(int customerId) {
		CustomerManager.customerId = customerId;
	}

	public HashMap<String, Customer> getCustomerMap() {
		return customerMap;
	}

	public void setCustomerMap(HashMap<String, Customer> customerMap) {
		CustomerManager.customerMap = customerMap;
	}
}
